package br.com.agiledev.webscrum.entity;

import static javax.persistence.CascadeType.REMOVE;
import static javax.persistence.FetchType.LAZY;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;

/**
 * Entity implementation class for Entity: Product
 * 
 */
@Entity
@SequenceGenerator(name = "product", sequenceName = "se_product")
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product")
    private Long oid;

    @Column(nullable = false, length = 50)
    private String nome;

    @Column(length = 255)
    private String descricao;

    @OrderBy("dataInicio ASC")
    @OneToMany(mappedBy = "product", fetch = LAZY, cascade = REMOVE)
    private List<Release> releases;
    
    @OneToMany(cascade = REMOVE, fetch = LAZY, mappedBy = "product")
    private List<ItemProduct> itens;

    public Product() {
	super();
    }

    public List<ItemProduct> getItens() {
	return itens;
    }

    public Long getOid() {
	return this.oid;
    }

    public List<Release> getReleases() {
	return releases;
    }

    public void setItens(List<ItemProduct> itens) {
	this.itens = itens;
    }

    public void setOid(Long oid) {
	this.oid = oid;
    }

    public void setOID(Long OID) {
	this.oid = OID;
    }

    public void setReleases(List<Release> releases) {
	this.releases = releases;
    }

    public String getNome() {
	return nome;
    }

    public void setNome(String nome) {
	this.nome = nome;
    }

    public String getDescricao() {
	return descricao;
    }

    public void setDescricao(String descricao) {
	this.descricao = descricao;
    }

    public void removeItem(Item item) {

	if (itens == null) {
	    return;
	}

	ItemProduct ip = null;
	for (Iterator<ItemProduct> iterator = itens.iterator(); iterator.hasNext();) {
	    ip = iterator.next();
	    if (ip.getItem().equals(item)) {
		iterator.remove();
		break;
	    }
	}
    }

    @Override
    public String toString() {
	return "Product [descricao=" + descricao + ", nome=" + nome + ", oid=" + oid + "]";
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((descricao == null) ? 0 : descricao.hashCode());
	result = prime * result + ((itens == null) ? 0 : itens.hashCode());
	result = prime * result + ((nome == null) ? 0 : nome.hashCode());
	result = prime * result + ((oid == null) ? 0 : oid.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	Product other = (Product) obj;
	if (oid == null) {
	    if (other.oid != null)
		return false;
	} else if (!oid.equals(other.getOid()))
	    return false;
	return true;
    }
}
